<?php
/**
 * Panel de aprobación de comentarios.
 * @author  Sahib J. Leo
 */
require '../../config.php';
require PATH_ADMIN . 'session.php';
require PATH . 'classes/ofj-orm/Comentario.php';


define('MENU_ACTIVE', 'comentarios');

$css_arr = array('comentarios/comentarios.css');
require PATH . 'admin/layout/header.php';

/*
 * Definir el tipo de comentarios, el tipo nos dice que comentarios
 * se van a cargar, tambien nos dice cual boton del header estara
 * marcado como seleccionado.
 */
if( !isset($_GET['type']) ){
  $comments_type = 'all';
} else {
  $comments_type = $_GET['type'] == 'all' ? 'all'
    : ($_GET['type'] == 'eventos' ? 'eventos' : 'noticias');
}

?>
<div class="container">
  <div class="well">
    <p>Selecciona el tipo de comentarios que deseas moderar.</p>
    <a href="comentarios/?type=all" class="btn <?php
      echo $comments_type == 'all' ? 'btn-success' : ''
    ?>">Ver todos</a>
    <a href="comentarios/?type=eventos" class="btn <?php
      echo $comments_type == 'eventos' ? 'btn-success' : ''
    ?>">Eventos</a>
    <a href="comentarios/?type=noticias" class="btn <?php
      echo $comments_type == 'noticias' ? 'btn-success' : ''
    ?>">Noticias</a>
  </div>
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th class="row_author">Autor</th>
        <th class="row_email">EMail</th>
        <th class="row_comment">Comentario</th>
        <th class="row_type">Tipo</th>
        <th>Editar</th>
      </tr>
    </thead>
    <tbody>
      <?php
      /*
       * Traer la lista de comentarios
       */
      $page              = 1;
      $total_pages       = 1;
      $comments_per_page = 10;

      if( isset($_GET['page']) ){
        settype($_GET['page'], 'int');
        $page = $_GET['page'] > 0 ? $_GET['page'] : 1;
      }

      // Formar el WHERE para la seleccion de comentarios
      $where = null;
      if( $comments_type == 'eventos' ){
        $where = "`evento_id` IS NOT NULL";
      } else if( $comments_type == 'noticias' ){
        $where = "`noticia_id` IS NOT NULL";
      }

      // Calcular el número de paginas para la paginación
      $total_pages = ceil(Comentario::instance()->count(array(
        'where' => $where
      )) / $comments_per_page);

      // Traer lista de comentarios
      $comments = Comentario::instance()->find(array(
        'where'    => $where,
        'limit'    => $comments_per_page,
        'skip'     => ($page - 1) * $comments_per_page,
        'order_by' => '`estado` DESC, `id` DESC'
      ));

      if( count($comments) == 0 ):
      ?>
      <tr>
        <td colspan="5">Ningún comentario encontrado.</td>
      </tr>
      <?php
      else:
        foreach($comments as $Comentario):
      ?>
      <tr id="row_comment_<?php echo $Comentario->id ?>">
        <td><?php
          echo htmlentities($Comentario->usuario, ENT_QUOTES, 'utf-8')
        ?></td>
        <td><?php
          echo htmlentities($Comentario->email, ENT_QUOTES, 'utf-8')
        ?></td>
        <td><div class="comment"><?php
          echo nl2br(htmlentities($Comentario->comentario, ENT_QUOTES, 'utf-8'))
        ?>
        </div></td>
        <td class="center"><?php
          if( $Comentario->evento_id != null ):
          ?><a
            href="<?php echo BASE, 'conciertos-eventos/evento/?id='
            , $Comentario->evento_id ?>"
            target="_blank">Evento</a>
          <?php
          else:
          ?><a
            href="<?php echo BASE, 'noticias/?id=', $Comentario->noticia_id ?>"
            target="_blank">Noticia</a>
          <?php
          endif;
        ?></td>
        <td>
          <div class="btn-toolbar">
            <div class="btn-group">
              <?php if($Comentario->estado == 'E'): ?>
              <a href="#" class="btn btn-success" title="Aprobar / publicar"
                data-approve="<?php echo $Comentario->id ?>">
                <i class="icon-ok icon-white"></i>
              </a>
              <?php endif ?>
              <a href="#" class="btn btn-danger dropdown-toggle"
                title="Rechazar / borrar" data-toggle="dropdown">
                <i class="icon-trash icon-white"></i>
                <span class="caret"></span>
              </a>
              <ul class="dropdown-menu">
                <li class="dropdown-menu-title">¿Confirma borrar?</li>
                <li><a href="#"
                  data-delete="<?php echo $Comentario->id ?>">Sí, borrar.</a></li>
                <li><a href="javascript:;">No.</a></li>
              </ul>
            </div>
          </div>
        </td>
      </tr>
      <?php
        endforeach;
      endif;
      /* END: if( count($comments) == 0 ): */
      ?>
    </tbody>
  </table>
  <?php
  // Configuración para los botones de paginación atras y adelante
  $prev_disabled = null;
  $next_disabled = null;
  $prev_href     = 'comentarios/?page=' . ($page - 1) . '&type=' . $comments_type;
  $next_href     = 'comentarios/?page=' . ($page + 1) . '&type=' . $comments_type;
  
  if($page <= 1){
    $prev_disabled = 'class="disabled"';
    $prev_href = 'javascript:;';
  }

  if($page >= $total_pages){
    $next_disabled = 'class="disabled"';
    $next_href = 'javascript:;';
  }
  ?>
  <div class="pagination pagination-right">
    <ul>
      <li <?php echo $prev_disabled ?>><a href="<?php
        echo $prev_href ?>">&laquo;</a></li>
      <?php
      for($i = 1; $i <= $total_pages; $i++):
        // Configuración para el boton de paginado
        $page_href = 'comentarios/?page=' . $i . '&type=' . $comments_type;
        $page_active = null;
        
        if($i == $page){
          $page_href   = 'javascript:;';
          $page_active = 'class="active"';
        }
      ?>
      <li <?php echo $page_active ?>><a href="<?php echo $page_href ?>"><?php
        echo $i ?></a></li>
      <?php endfor ?>
      <li <?php echo $next_disabled ?>><a href="<?php
        echo $next_href ?>">&raquo;</a></li>
    </ul>
  </div>
  <!-- // paginado -->
</div>
<?php
$js_arr = array('bootstrap-dropdown.js', 'comentarios/comentarios.js');
require PATH . 'admin/layout/footer.php';
?>